/*
 * Copyright 2008-2009 Daniel Freitas
 * 
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Army Creator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Army Creator. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dmf.genericdao.test;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.googlecode.dmf.genericdao.GenericDao;

/**
 * Simulates a service that delimites a transaction boundary.
 * 
 * @author <a href="mailto:daniel.mfreitas@gmail.com">Daniel Freitas</a>
 */
@Component
public class TransactionTestService implements TransactionService {

    @Resource
    private final GenericDao<PersistentDummy> persistentDummyDao;

    @Autowired
    public TransactionTestService(final GenericDao<PersistentDummy> genericDao) {
        persistentDummyDao = genericDao;
    }

    @Override
    public void updateTwoObjectsAndThrowsRumtimeException(final PersistentDummy object1, final PersistentDummy object2) {
        persistentDummyDao.makePersistent(object1);
        persistentDummyDao.makePersistent(object2);

        throw new IllegalArgumentException("You should rollback");
    }

    @Override
    public void updateTwoObjectsAndThrowsCheckedException(final PersistentDummy object1, final PersistentDummy object2)
            throws Exception {
        persistentDummyDao.makePersistent(object1);
        persistentDummyDao.makePersistent(object2);

        throw new Exception("Although this exception is thrown, the changes "
                + "should have been committed since this was a checked exception.");
    }

    @Override
    public void updateTwoObjectsAndThrowsCheckedExceptionAnnotatedForRollback(final PersistentDummy object1,
            final PersistentDummy object2) throws Exception {
        persistentDummyDao.makePersistent(object1);
        persistentDummyDao.makePersistent(object2);

        throw new Exception("Ok. This is a checked exception. The default behaviour is to not rollback on checked "
                + "exception, but since we annotated it to do so the transaction should have been rolled back.");
    }
}
